package com.yellowpage.tool;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by Sun on 12/4/2015.
 */
public class UserGet {

    //获取邮件列表
    //参数1 type 表示了请求的类型，1为APP请求  2为能力调用
    //参数2 tool 表示了发送途径，  1为email,   2为电话
    public static List<String> getUserList(int type,int tool)

    {
        //读取数据库配置
        InputStream in = UserGet.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties props = new Properties();
        try {
            props.load(in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String driver = props.getProperty("jdbc.driverClassName");
        String url = props.getProperty("jdbc.url");
        String username = props.getProperty("jdbc.username");
        String password = props.getProperty("jdbc.password");
        Connection conn = null;
        PreparedStatement statement = null;
        List<String> userlists = new ArrayList<String>();


        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
            String sql = "SELECT u.EMAIL,u.TELEPHONE FROM AUTO_MAIL_USER u WHERE u.TYPE = 0 or u.TYPE = ?";
            statement = conn.prepareStatement(sql);

            //进行类型选择
            statement.setInt(1,type);
            ResultSet rs = statement.executeQuery();

            while (rs.next()) {
                String result = null;
                if(tool == 1)
                    result = rs.getString("u.EMAIL");
                else if (tool == 2)
                    result = rs.getString("u.TELEPHONE");

                userlists.add(result);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return userlists;

    }

}